using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CPGVertragsverwaltung.DatabaseAccess;

namespace CPGVertragsverwaltung.Viewer
{
    public partial class MiniObjectBar : UserControl
    {
        private MiniObjectGroupCollection mGroups = new MiniObjectGroupCollection();

        #region Events
        public delegate void ItemSelectedDelegate(MiniObjectItem pItem);
        public event ItemSelectedDelegate ItemSelected;
        #endregion

        #region Property
        public MiniObjectGroupCollection Groups
        {
            set
            {
                mGroups = value;
            }
            get {
                return mGroups;
            }
        }
        #endregion

        #region private function
        /// <summary>
        /// Get Index from Group contains Key
        /// </summary>
        /// <param name="pKey">Key</param>
        /// <returns>-1 if not found</returns>
        private int getIndexByKey(String pKey)
        {
            int result = -1;
            foreach (MiniObjectGroup group in mGroups)
            {
                if (group.Key == pKey)
                {
                    result = mGroups.IndexOf(group);
                }
            }

            return result;
        }

        private void MiniObjectBar_Resize(object sender, EventArgs e)
        {
            redrawItems();
        }

        
        #endregion

        public MiniObjectBar()
        {
            InitializeComponent();
        }

        /// <summary>
        /// redraw all Items
        /// </summary>
        public void redrawItems()
        {
            int currentHeight = 0;
            if (mGroups != null)
            {
                for (int i = 0; i < mGroups.Count; i++)
                {
                    MiniObjectGroup myGroup = mGroups[i];
                    myGroup.Top = currentHeight;
                    if (this.VerticalScroll.Enabled)
                    {
                        myGroup.Width = this.Width - 20;
                    }
                    else
                    {
                        myGroup.Width = this.Width;
                    }

                    currentHeight += myGroup.Height;
                }
            }
        }

        /// <summary>
        /// Add Group to Array
        /// </summary>
        /// <param name="pGroup">Group</param>
        public void addGroup(MiniObjectGroup pGroup)
        {
            pGroup.ItemSelected += new MiniObjectGroup.ItemSelectedDelegate(OnItemSelected);
            mGroups.Add(pGroup);
            this.Controls.Add(pGroup);
            this.redrawItems();
        }

        /// <summary>
        /// Delete Group by Index
        /// </summary>
        /// <param name="pIndex">Index</param>
        public void deleteGroup(int pIndex)
        {
            Groups[pIndex].Dispose();
            mGroups.RemoveAt(pIndex);
            redrawItems();
        }

        /// <summary>
        /// clear all Groups, redrawn all
        /// </summary>
        public void clearGroups()
        {
            foreach (MiniObjectGroup objGroup in Groups)
            {
                objGroup.Dispose();
            }
            Groups.Clear();
            redrawItems();
            Application.DoEvents();
        }

        /// <summary>
        /// Delete Group By Key
        /// </summary>
        /// <param name="pKey">key</param>
        public void deleteGroup(String pKey)
        {
            int Index = getIndexByKey(pKey);
            deleteGroup(Index);
        }

        /// <summary>
        /// Search every Group for this Item and delete it.
        /// </summary>
        /// <param name="pItem">Item to be deleted</param>
        public void deleteItem(MiniObjectItem pItem)
        {
            foreach (MiniObjectGroup objGroup in Groups)
            {
                objGroup.deleteItem(pItem);
            }
            redrawItems();
        }

        private void MiniObjectBar_Load(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// do something when an item is Selected
        /// </summary>
        /// <param name="pItem">Selected Item</param>
        private void OnItemSelected(MiniObjectItem pItem)
        {
            //Deselect All other Item
            foreach (MiniObjectGroup group in mGroups)
            {
                group.deselectItemsExcept(pItem);
            }
            if (this.ItemSelected != null)
            {
                this.ItemSelected(pItem);
            }
        }
        
    }
}
